package cn.chenshi.excel;

import cn.chenshi.excel.model.ExcelLocalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ExcelService {

    @Autowired
    private ExcelLocalBean excelLocalBean;

    /**
     * 文件形式上传
     *
     * @param key
     * @param file
     * @return
     */
    public String upload(String key, MultipartFile file) {
        Map<String, Object> result = new HashMap<String, Object>();
        String fileName = "";
        try {
            if (file != null) {
                //获取上传文件名
                fileName = file.getOriginalFilename();
                //获取后缀名
                String sname = fileName.substring(fileName.lastIndexOf("."));
                //获取当前时间并作为时间戳
                String timeStamp = new SimpleDateFormat("yyyy-MM-ddHHmmssSSS").format(new Date());
                //指定上传文件的路径
                String path = "D:\\" + timeStamp + fileName + sname;
                //上传保存
                file.transferTo(new File(path));
            }
            result.put("statusCode", "200");
            result.put("message", "上传成功!");
            result.put("filename", fileName);
        } catch (Exception ex) {
            result.put("statusCode", "300");
            result.put("message", "上传失败:" + ex.getMessage());
        }
        return null;
    }

    /**
     * 流式上传
     *
     * @param key
     * @param inputStream
     * @return
     */
    public String upload(String key, InputStream inputStream) {
        return null;
    }

    /**
     * 下载文件
     *
     * @param key
     * @return
     * @throws IOException
     */
    public BufferedReader download(String key) throws IOException {
        return null;
    }

    /**
     * 文件是否存在
     *
     * @param key
     */

    public boolean exist(String key) {
        return false;
    }

    /**
     * 删除文件
     *
     * @param key
     */
    public void delete(String key) {
    }

}